import requests
from bs4 import BeautifulSoup
import pymysql

#连接数据库
conn = pymysql.connect(
    host='local'
    user='root'
    password='your_password',
    database='test_db',
    charset='utf8mb4'
)
cursor = conn.cursor()

# 爬取数据（以爬取网页标题为例）
url = 'https://example.com'
   response = requests.get(url)
   soup = BeautifulSoup(response.text, 'html.parser')
   title = soup.title.text.strip() if soup.title else None
   client = MongoClient('mongodb://localhost:27017/')
   db = client['test_db']
   collection = db['articles']
        
# 爬取数据
 url = 'https://example.com'
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    data = {
    'title': soup.title.text.strip() if soup.title else None,
    'url': url
        }
        
# 写入数据库
try:
collection.insert_one(data)
print("数据写入成功")
except Exception as e:
print(f"错误：{e}")
finally:
client.close()